function  [x_all_matrix,y_all_matrix ]= exh_apeo_data(n, min_runs,max_runs,x_k_min ,x_k_max , x_prefix_length,y_k_min ,y_k_max , y_prefix_length)
    x_all_matrix = false(0, n);  % 存储所有 x 矩阵
    y_all_matrix = false(0, n); % 存储所有 y 矩阵

    % 生成 x 矩阵
    parfor k = x_k_min:x_k_max
        % 生成组合和二进制映射
        result_map = generateCombinationsAndBinaryMap(n, k, x_prefix_length);
        % 将二进制映射转换为矩阵
        x_matrix = convertMapToMatrix(result_map,0,min_runs,max_runs);
        x_all_matrix = vertcat(x_all_matrix, x_matrix); % 添加到总 x 矩阵
    end

    % 生成 y 矩阵
    parfor k = y_k_min:y_k_max
        % 生成组合和二进制映射
        result_map = generateCombinationsAndBinaryMap(n, k, y_prefix_length);
        % 将二进制映射转换为矩阵
        y_matrix = convertMapToMatrix(result_map,1,min_runs,max_runs);
        y_all_matrix = [y_all_matrix; y_matrix];  % 添加到总 y 矩阵
    end
end
function  min_seq = check_equivalent_binary_sequence(x, yset)
% 获取序列长度
n = length(x);
x = x(:)'; % 确保 x 是行向量

% 自身变换 (I)
I = x;
%if ~iskeys(num2str(I)) result=true;return;end

% 翻转 (C)
C = ~x;

% 偶数位翻转 (A)
A = x;
A(2:2:end) = ~x(2:2:end); % 翻转奇数位

% 反转 (R)
R = fliplr(x);

% 初始化等价序列集合
equivalent_sequences = {I, R, C, A}; % 包含基础变换

if mod(n, 2) == 0 % n 为偶数
    % RA: 先反转 R
    RA = R;
    RA(2:2:end) = ~RA(2:2:end); % 然后进行奇数位翻转

    % AR: 先对 A 进行奇数位翻转
    AR = A;
    AR = fliplr(AR); % 然后进行反转

    RC=R;
    RC = ~RC; % RC: C 的反转

    % CA: 先反转 C
    CA = C;
    CA(2:2:end) = ~C(2:2:end); % 在反转后的基础上进行奇数位翻转

    equivalent_sequences = [equivalent_sequences, RA, AR, RC, CA];
else % n 为奇数
    RC = ~R; % RC: R 的反转

    RA = R;
    RA(2:2:end) = ~RA(2:2:end); % RA: R 的反转

    % CA: 先反转 C
    CA = C;
    CA(2:2:end) = ~CA(2:2:end); % 在反转后的基础上进行奇数位翻转


    RCA = ~RC(2:2:end); % RCA: CA 的反转

    equivalent_sequences = [equivalent_sequences, RC, RA, CA, RCA];
end



min_seq2num = inf;
min_seq = x;


for i=1 :length(equivalent_sequences)
    seq=equivalent_sequences{i};
    keyString = num2str(seq); % 转换为字符串
    if isKey(yset, keyString)

       if bin2dec(keyString) < min_seq2num
                min_seq = seq;
                min_seq2num = bin2dec(num2str(seq));
        end
        yset(keyString) = [];
    end
end


end

function valid = is_valid_runs(seq,min_runs,max_runs)


total_runs = 1;
current_value = seq(1);

for i = 2:length(seq)
    if seq(i) ~= current_value
        total_runs = total_runs + 1;
        current_value = seq(i);
    end
end

valid = (total_runs < min_runs || total_runs > max_runs);
end
function binary_map = generateCombinationsAndBinaryMap(n, k, prefix_length)
% 生成 n 选 k 的所有组合并返回二进制映射
combinations = nchoosek(1:n-prefix_length, k-prefix_length);  % 计算组合
binary_map = containers.Map(); % 初始化映射

% 为每个组合创建二进制字符串
num_combinations = size(combinations, 1);  % 获取组合数量
binary_string_template = zeros(1, n-prefix_length);  % 创建二进制字符串模板
prefix_array = ones(1, prefix_length);
for i = 1:num_combinations
    combination = combinations(i, :);  % 当前组合

    binary_array = binary_string_template;  % 用模板初始化二进制字符串
    binary_array(combination) = 1;  % 设置与组合对应的位置为 1
    x = [prefix_array, binary_array];  % 与前缀组合

    % 将二进制数组转换为字符串
    binary_string = num2str(x);

    % 将二进制字符串作为键存储在映射中，并给定其值
    binary_map(binary_string) = x ;
end
end

function binary_matrix = convertMapToMatrix(binary_map, bj,min_runs,max_runs)
% 将二进制映射转换为行数等于映射键计数的矩阵
keys = binary_map.keys();
binary_matrix = false(length(keys), length(binary_map(keys{1})));  % 初始化结果矩阵

row_index = 1;  % 矩阵行索引

for i = 1:length(keys)
    current_key = binary_map(keys{i});
    if ~isempty(current_key)  % 检查值是否存在

        if is_valid_runs(current_key,min_runs,max_runs),continue;end
        binary_matrix(row_index, :) = current_key;
        if bj
            binary_matrix(row_index, :) = check_equivalent_binary_sequence(current_key,binary_map); 
        end
        row_index = row_index + 1;  % 移动到下一行
    end
end

% 移除未使用的行
binary_matrix = binary_matrix(1:row_index-1, :);  % 仅保留有效行
end
